import re

# 2017-11-29
p = '\d{4}-\d{1,2}-\d{1,2}'
p = '[1-9]\d{3}-(1[012]|[1-9])-([12][0-9]|3[01])'
# s = '2017-11-29'
s = '1017-2-31'
res = re.match(p,s)
print(res.group())
